<template>
  <div class="doctor-profile">
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>个人信息</h3>
          <el-button type="primary" @click="editMode = true" v-if="!editMode">编辑</el-button>
          <div v-else>
            <el-button type="success" @click="saveProfile">保存</el-button>
            <el-button @click="cancelEdit">取消</el-button>
          </div>
        </div>
      </template>
      
      <el-form :model="profileForm" label-width="120px" v-if="editMode">
        <el-form-item label="用户名">
          <el-input v-model="profileForm.username" disabled />
        </el-form-item>
        
        <el-form-item label="真实姓名" required>
          <el-input v-model="profileForm.realName" />
        </el-form-item>
        
        <el-form-item label="手机号" required>
          <el-input v-model="profileForm.phone" />
        </el-form-item>
        
        <el-form-item label="医院" required>
          <el-input v-model="profileForm.hospital" />
        </el-form-item>
        
        <el-form-item label="科室" required>
          <el-input v-model="profileForm.department" />
        </el-form-item>
        
        <el-form-item label="职称" required>
          <el-input v-model="profileForm.title" />
        </el-form-item>
        
        <el-form-item label="执业编号" required>
          <el-input v-model="profileForm.license" />
        </el-form-item>
      </el-form>
      
      <el-descriptions :column="2" border v-else>
        <el-descriptions-item label="用户名">{{ profileForm.username }}</el-descriptions-item>
        <el-descriptions-item label="真实姓名">{{ profileForm.realName }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ profileForm.phone }}</el-descriptions-item>
        <el-descriptions-item label="医院">{{ profileForm.hospital }}</el-descriptions-item>
        <el-descriptions-item label="科室">{{ profileForm.department }}</el-descriptions-item>
        <el-descriptions-item label="职称">{{ profileForm.title }}</el-descriptions-item>
        <el-descriptions-item label="执业编号">{{ profileForm.license }}</el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useAuthStore } from '@/store'
import { ElMessage } from 'element-plus'

const authStore = useAuthStore()
const editMode = ref(false)

// 初始化个人信息表单
const profileForm = ref({
  username: '',
  realName: '',
  phone: '',
  hospital: '',
  department: '',
  title: '',
  license: ''
})

onMounted(() => {
  // 从状态管理中加载用户信息
  if (authStore.userInfo) {
    profileForm.value = { ...authStore.userInfo }
  }
})

const saveProfile = () => {
  // 保存个人信息
  authStore.setUserInfo(profileForm.value)
  editMode.value = false
  ElMessage.success('个人信息已保存')
}

const cancelEdit = () => {
  // 取消编辑，恢复原始数据
  if (authStore.userInfo) {
    profileForm.value = { ...authStore.userInfo }
  }
  editMode.value = false
}
</script>

<style scoped>
.doctor-profile {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-form {
  max-width: 800px;
}

.el-descriptions {
  margin-top: 20px;
}
</style>